<?php

namespace App\Modules\Health\Http\Controllers\Admin;

use App\Modules\Health\Extensions\Tools\BatchChangeDiseaseStatus;
use App\Modules\Health\Repositories\Disease;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Arr;

class DiseaseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Disease(['class']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');

            $grid->column('id');
            $grid->column('class.name', '分类');
            $grid->column('name');
            $grid->column('img')->image(' ', 50, 50);
            $grid->column('is_show')->switch();
            $grid->column('created_at');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append('<a style="margin-right:5px" href="' . admin_route('health.disease-scheme.index', ['disease_id' => $actions->row->id]) . '">调理方案</a>');
            });

            $grid->batchActions([
                new BatchChangeDiseaseStatus('批量将状态变更为显示', 1),
                new BatchChangeDiseaseStatus('批量将状态变更为隐藏', 0),
            ]);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->equal('class_id', '分类')->select(
                    \App\Modules\Health\Models\DiseaseClass::pluck('name', 'id')->all()
                )->width(3);
                $filter->like('name')->width(3);
                $filter->equal('is_show')->select([0 => '隐藏', 1 => '显示'])->width(3);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Disease(), function (Show $show) {
            $show->field('id');
            $show->field('class_id');
            $show->field('name');
            $show->field('img');
            $show->field('introduce');
            $show->field('treat_details');
            $show->field('clinical_experience');
            $show->field('case');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Disease(), function (Form $form) {
            $form->display('id');
            $form->select('class_id')->options(
                \App\Modules\Health\Models\DiseaseClass::pluck('name', 'id')->all()
            )->required();
            $form->text('name')->required();
            $form->image('img')->required()->autoUpload()->saveFullUrl()->uniqueName();
            $form->editor('introduce')->required();
//            $form->hasMany('scheme', '调理方案', function (Form\NestedForm $form){
//                $form->select('acu_id', '穴位')->options(
//                    \App\Modules\Health\Models\Acupoint::pluck('name', 'id')->all()
//                )->required();
//                $form->text('description', '穴数')->required();
//                $form->number('day', '天数')->required();
//                $form->number('time', '时长(分钟)')->required();
//            });
            $form->editor('treat_details');
            $form->editor('clinical_experience');
            $form->editor('case');
            $form->radio('is_show')->options(['隐藏', '显示'])->default(1);

            $form->deleted(function (Form $form, $result){
                if ($result) {
                    $ids = Arr::pluck($form->model()->toArray(), 'id');
                    \App\Modules\Health\Models\DiseaseScheme::whereIn('disease_id', $ids)->delete();
                }
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
